#include <n32g45x_tim.h>
#include <tim.h>


void times_init(void)
{
    tim1_init();
}

void tim1_init(void)
{
    /* TIM1 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
    TIM_ConfigPrescaler(TIM1, 2, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    NVIC_InitType NVIC_InitStructure;

    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.RepetCnt = 0;
    TIM_TimeBaseStructure.Period    = 143;
    TIM_TimeBaseStructure.Prescaler = 1000;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);

    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE, ENABLE);

    /* TIM1 enable counter */
    TIM_Enable(TIM1, ENABLE);

}
